 package server;

 import client.MapleCharacter;
 import client.MapleClient;
 import client.inventory.Item;
 import client.inventory.ItemFlag;
 import client.inventory.MapleInventoryType;
 import constants.GameConstants;
 import constants.ItemConstants;
 import handling.world.WorldBroadcastService;
 import java.lang.ref.WeakReference;
 import java.util.LinkedList;
 import java.util.List;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.commands.CommandProcessor;
 import server.commands.CommandType;
 import tools.MaplePacketCreator;
 import tools.packet.PlayerShopPacket;
 import tools.packet.TradePacket;


 public class MapleTrade
 {
/*  25 */   private static final Logger log = LogManager.getLogger(MapleTrade.class);
/*  26 */   private final List<Item> items = new LinkedList<>();
   private final WeakReference<MapleCharacter> chr;
   private final byte tradingslot;
/*  29 */   private MapleTrade partner = null;
   private List<Item> exchangeItems;
/*  31 */   private long meso = 0L; private long exchangeMeso = 0L;
   private boolean locked = false, inTrade = false;

   public MapleTrade(byte tradingslot, MapleCharacter chr) {
/*  35 */     this.tradingslot = tradingslot;
/*  36 */     this.chr = new WeakReference<>(chr);
   }






   public static void completeTrade(MapleCharacter player) {
/*  45 */     MapleTrade local = player.getTrade();
/*  46 */     MapleTrade partner = local.getPartner();
/*  47 */     if (partner == null || local.locked) {
       return;
     }
/*  50 */     local.locked = true;
/*  51 */     partner.getChr().getClient().announce(TradePacket.getTradeConfirmation());
/*  52 */     partner.exchangeItems = new LinkedList<>(local.items);
/*  53 */     partner.exchangeMeso = local.meso;
/*  54 */     if (partner.isLocked()) {
/*  55 */       int lz = local.check(), lz2 = partner.check();
/*  56 */       if (lz == 0 && lz2 == 0) {
/*  57 */         log.info("[交易] -------------------------------------------------------------------------- ");
/*  58 */         local.CompleteTrade();
/*  59 */         partner.CompleteTrade();
/*  60 */         log.info("[交易] " + local.getChr().getName() + " 和 " + partner.getChr().getName() + " 交易完成。");
       } else {

/*  63 */         partner.cancel(partner.getChr().getClient(), partner.getChr(), (lz == 0) ? lz2 : lz);
/*  64 */         local.cancel(player.getClient(), player, (lz == 0) ? lz2 : lz);
       }
/*  66 */       partner.getChr().setTrade(null);
/*  67 */       player.setTrade(null);
     }
   }




   public static void cancelTrade(MapleTrade Localtrade, MapleClient c, MapleCharacter player) {
/*  75 */     Localtrade.cancel(c, player);
/*  76 */     MapleTrade partner = Localtrade.getPartner();
/*  77 */     if (partner != null && partner.getChr() != null) {
/*  78 */       partner.cancel(partner.getChr().getClient(), partner.getChr());
/*  79 */       partner.getChr().setTrade(null);
     }
/*  81 */     player.setTrade(null);
   }





   public static void startTrade(MapleCharacter player) {
/*  89 */     if (player.getTrade() == null) {
/*  90 */       player.setTrade(new MapleTrade((byte)0, player));
/*  91 */       player.getClient().announce(TradePacket.getTradeStart(player.getClient(), player.getTrade(), (byte)0));
     } else {
/*  93 */       player.getClient().announce(MaplePacketCreator.serverNotice(5, "不能同时做多件事情。"));
     }
   }




   public static void inviteTrade(MapleCharacter player, MapleCharacter target) {
/* 101 */     if (player == null || player.getTrade() == null) {
       return;
     }
/* 104 */     if (target != null && target.getTrade() == null) {
/* 105 */       target.setTrade(new MapleTrade((byte)1, target));
/* 106 */       target.getTrade().setPartner(player.getTrade());
/* 107 */       player.getTrade().setPartner(target.getTrade());
/* 108 */       target.getClient().announce(TradePacket.getTradeInvite(player));
     } else {
/* 110 */       player.getClient().announce(MaplePacketCreator.serverNotice(5, "对方正在和其他玩家进行交易中。"));
/* 111 */       cancelTrade(player.getTrade(), player.getClient(), player);
     }
   }




   public static void visitTrade(MapleCharacter player, MapleCharacter target) {
/* 119 */     if (target != null && player.getTrade() != null && player.getTrade().getPartner() == target.getTrade() && target.getTrade() != null && target.getTrade().getPartner() == player.getTrade()) {
/* 120 */       (player.getTrade()).inTrade = true;
/* 121 */       target.getClient().announce(PlayerShopPacket.playerInterVisitorAdd(player, 1));
/* 122 */       player.getClient().announce(TradePacket.getTradeStart(player.getClient(), player.getTrade(), (byte)1));
/* 123 */       player.dropMessage(-2, "系统提示 : 交易时请仔细查看交易的道具信息");
/* 124 */       target.dropMessage(-2, "系统提示 : 交易时请仔细查看交易的道具信息");
     } else {
/* 126 */       player.getClient().announce(MaplePacketCreator.serverNotice(5, "对方已经取消了交易。"));
     }
   }




   public static void declineTrade(MapleCharacter player) {
/* 134 */     MapleTrade trade = player.getTrade();
/* 135 */     if (trade != null) {
/* 136 */       if (trade.getPartner() != null) {
/* 137 */         MapleCharacter other = trade.getPartner().getChr();
/* 138 */         if (other != null && other.getTrade() != null) {
/* 139 */           other.getTrade().cancel(other.getClient(), other);
/* 140 */           other.setTrade(null);
/* 141 */           other.dropMessage(5, player.getName() + " 拒绝了你的交易邀请。");
         }
       }
/* 144 */       trade.cancel(player.getClient(), player);
/* 145 */       player.setTrade(null);
     }
   }




   public void CompleteTrade() {
/* 153 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 154 */     if (this.exchangeItems != null) {
/* 155 */       List<Item> itemz = new LinkedList<>(this.exchangeItems);
/* 156 */       for (Item item : itemz) {
/* 157 */         short flag = item.getFlag();
/* 158 */         if (ItemFlag.可以交换1次.check(flag)) {
/* 159 */           item.setFlag((short)(flag - ItemFlag.可以交换1次.getValue()));
/* 160 */         } else if (ItemFlag.宿命剪刀.check(flag)) {
/* 161 */           item.setFlag((short)(flag - ItemFlag.宿命剪刀.getValue()));
         }
/* 163 */         MapleInventoryManipulator.addFromDrop(((MapleCharacter)this.chr.get()).getClient(), item, false);
/* 164 */         log.info("[交易] " + ((MapleCharacter)this.chr.get()).getName() + " 交易获得道具: " + item.getItemId() + " x " + item.getQuantity() + " - " + ii.getName(item.getItemId()));
       }
/* 166 */       this.exchangeItems.clear();
     }
/* 168 */     if (this.exchangeMeso > 0L) {
/* 169 */       ((MapleCharacter)this.chr.get()).gainMeso(this.exchangeMeso - GameConstants.getTaxAmount(this.exchangeMeso), false, false);
/* 170 */       log.info("[交易] " + ((MapleCharacter)this.chr.get()).getName() + " 交易获得金币: " + this.exchangeMeso);
     }
/* 172 */     this.exchangeMeso = 0L;
/* 173 */     ((MapleCharacter)this.chr.get()).getClient().announce(TradePacket.TradeMessage(this.tradingslot, (byte)7));
   }





   public void cancel(MapleClient c, MapleCharacter chr) {
/* 181 */     cancel(c, chr, 0);
   }










   public void cancel(MapleClient c, MapleCharacter chr, int message) {
/* 194 */     if (this.items != null) {
/* 195 */       List<Item> itemz = new LinkedList<>(this.items);
/* 196 */       for (Item item : itemz) {
/* 197 */         MapleInventoryManipulator.addFromDrop(c, item, false);
       }
/* 199 */       this.items.clear();
     }
/* 201 */     if (this.meso > 0L) {
/* 202 */       chr.gainMeso(this.meso, false, false);
     }
/* 204 */     this.meso = 0L;
/* 205 */     c.announce(TradePacket.getTradeCancel(this.tradingslot, message));
   }




   public boolean isLocked() {
/* 212 */     return this.locked;
   }




   public void setMeso(long meso) {
/* 219 */     if (this.locked || this.partner == null || meso <= 0L || this.meso + meso <= 0L) {
       return;
     }
/* 222 */     if (((MapleCharacter)this.chr.get()).getMeso() >= meso) {
/* 223 */       ((MapleCharacter)this.chr.get()).gainMeso(-meso, false, false);
/* 224 */       this.meso += meso;
/* 225 */       ((MapleCharacter)this.chr.get()).getClient().announce(TradePacket.getTradeMesoSet((byte)0, this.meso));
/* 226 */       if (this.partner != null) {
/* 227 */         this.partner.getChr().getClient().announce(TradePacket.getTradeMesoSet((byte)1, this.meso));
       }
     }
   }




   public void addItem(Item item) {
/* 236 */     if (this.locked || this.partner == null) {
       return;
     }
/* 239 */     this.items.add(item);
/* 240 */     ((MapleCharacter)this.chr.get()).getClient().announce(TradePacket.getTradeItemAdd((byte)0, item));
/* 241 */     if (this.partner != null) {
/* 242 */       this.partner.getChr().getClient().announce(TradePacket.getTradeItemAdd((byte)1, item));
     }
   }




   public void chat(String message) {
/* 250 */     if (!CommandProcessor.processCommand(((MapleCharacter)this.chr.get()).getClient(), message, CommandType.TRADE)) {
/* 251 */       ((MapleCharacter)this.chr.get()).dropMessage(-2, ((MapleCharacter)this.chr.get()).getName() + " : " + message);
/* 252 */       if (this.partner != null) {
/* 253 */         this.partner.getChr().getClient().announce(PlayerShopPacket.playerInterChat(((MapleCharacter)this.chr.get()).getName() + " : " + message, 1, ((MapleCharacter)this.chr.get()).getName()));
       }
     }
/* 256 */     if (((MapleCharacter)this.chr.get()).getClient().isMonitored()) {
/* 257 */       WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverNotice(6, ((MapleCharacter)this.chr.get()).getName() + " 在交易中对 " + this.partner.getChr().getName() + " 说: " + message));
/* 258 */     } else if (this.partner != null && this.partner.getChr() != null && this.partner.getChr().getClient().isMonitored()) {
/* 259 */       WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverNotice(6, ((MapleCharacter)this.chr.get()).getName() + " 在交易中对 " + this.partner.getChr().getName() + " 说: " + message));
     }
   }

   public void chatAuto(String message) {
/* 264 */     ((MapleCharacter)this.chr.get()).dropMessage(-2, message);
/* 265 */     if (this.partner != null) {
/* 266 */       this.partner.getChr().getClient().announce(PlayerShopPacket.playerInterChat(message, 1, ((MapleCharacter)this.chr.get()).getName()));
     }
/* 268 */     if (((MapleCharacter)this.chr.get()).getClient().isMonitored()) {
/* 269 */       WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverNotice(6, ((MapleCharacter)this.chr.get()).getName() + " said in trade [Automated] with " + this.partner.getChr().getName() + " 说: " + message));
/* 270 */     } else if (this.partner != null && this.partner.getChr() != null && this.partner.getChr().getClient().isMonitored()) {
/* 271 */       WorldBroadcastService.getInstance().broadcastGMMessage(MaplePacketCreator.serverNotice(6, ((MapleCharacter)this.chr.get()).getName() + " said in trade [Automated] with " + this.partner.getChr().getName() + " 说: " + message));
     }
   }

   public MapleTrade getPartner() {
/* 276 */     return this.partner;
   }

   public void setPartner(MapleTrade partner) {
/* 280 */     if (this.locked) {
       return;
     }
/* 283 */     this.partner = partner;
   }

   public MapleCharacter getChr() {
/* 287 */     return this.chr.get();
   }

   public int getNextTargetSlot() {
/* 291 */     if (this.items.size() >= 9) {
/* 292 */       return -1;
     }
/* 294 */     int ret = 1;
/* 295 */     for (Item item : this.items) {
/* 296 */       if (item.getPosition() == ret) {
/* 297 */         ret++;
       }
     }
/* 300 */     return ret;
   }

   public boolean inTrade() {
/* 304 */     return this.inTrade;
   }




   public boolean setItems(MapleClient c, Item item, byte targetSlot, int quantity) {
/* 311 */     int target = getNextTargetSlot();
/* 312 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 313 */     if (this.partner == null || target == -1 || ItemConstants.isPet(item.getItemId()) || isLocked() || (ItemConstants.getInventoryType(item.getItemId()) == MapleInventoryType.EQUIP && quantity != 1)) {
/* 314 */       return false;
     }
/* 316 */     short flag = item.getFlag();
/* 317 */     if (ItemFlag.不可交易.check(flag) || ItemFlag.封印.check(flag)) {
/* 318 */       c.announce(MaplePacketCreator.enableActions());
/* 319 */       return false;
     }
/* 321 */     if ((ii.isDropRestricted(item.getItemId()) || ii.isAccountShared(item.getItemId())) && 
/* 322 */       !ItemFlag.可以交换1次.check(flag) && !ItemFlag.宿命剪刀.check(flag)) {
/* 323 */       c.announce(MaplePacketCreator.enableActions());
/* 324 */       return false;
     }

/* 327 */     Item tradeItem = item.copy();
/* 328 */     if (ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
/* 329 */       tradeItem.setQuantity(item.getQuantity());
/* 330 */       MapleInventoryManipulator.removeFromSlot(c, ItemConstants.getInventoryType(item.getItemId()), item.getPosition(), item.getQuantity(), true);
     } else {
/* 332 */       tradeItem.setQuantity((short)quantity);
/* 333 */       MapleInventoryManipulator.removeFromSlot(c, ItemConstants.getInventoryType(item.getItemId()), item.getPosition(), (short)quantity, true);
     }
/* 335 */     if (targetSlot < 0) {
/* 336 */       targetSlot = (byte)target;
     } else {
/* 338 */       for (Item itemz : this.items) {
/* 339 */         if (itemz.getPosition() == targetSlot) {
/* 340 */           targetSlot = (byte)target;
           break;
         }
       }
     }
/* 345 */     tradeItem.setPosition((short)targetSlot);
/* 346 */     addItem(tradeItem);
/* 347 */     return true;
   }







   private int check() {
/* 357 */     if (((MapleCharacter)this.chr.get()).getMeso() + this.exchangeMeso < 0L) {
/* 358 */       return 1;
     }
/* 360 */     if (this.exchangeItems != null) {
/* 361 */       MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 362 */       byte eq = 0, use = 0, setup = 0, etc = 0, cash = 0;
/* 363 */       for (Item item : this.exchangeItems) {
/* 364 */         switch (ItemConstants.getInventoryType(item.getItemId())) {
           case EQUIP:
/* 366 */             eq = (byte)(eq + 1);
             break;
           case USE:
/* 369 */             use = (byte)(use + 1);
             break;
           case SETUP:
/* 372 */             setup = (byte)(setup + 1);
             break;
           case ETC:
/* 375 */             etc = (byte)(etc + 1);
             break;
           case CASH:
/* 378 */             cash = (byte)(cash + 1);
             break;
         }
/* 381 */         if (ii.isPickupRestricted(item.getItemId()) && ((MapleCharacter)this.chr.get()).haveItem(item.getItemId(), 1, true, true)) {
/* 382 */           return 2;
         }
       }
/* 385 */       if (((MapleCharacter)this.chr.get()).getInventory(MapleInventoryType.EQUIP).getNumFreeSlot() < eq || ((MapleCharacter)this.chr.get()).getInventory(MapleInventoryType.USE).getNumFreeSlot() < use || ((MapleCharacter)this.chr.get()).getInventory(MapleInventoryType.SETUP).getNumFreeSlot() < setup || ((MapleCharacter)this.chr.get()).getInventory(MapleInventoryType.ETC).getNumFreeSlot() < etc || ((MapleCharacter)this.chr.get()).getInventory(MapleInventoryType.CASH).getNumFreeSlot() < cash) {
/* 386 */         return 1;
       }
     }
/* 389 */     return 0;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\MapleTrade.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */